package com.cmccgz.udacs_landlord_webapp.adapter;


import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.WarningItem;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class WarningListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private final LayoutInflater inflater;
    private final SimpleDateFormat sdf;
    private List<WarningItem>datas;
    private Context context;
    private OnRvItemClickListenter<WarningItem> listenter;
    public WarningListAdapter(List<WarningItem> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
        sdf = new SimpleDateFormat("yyyy年MM月dd日");
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case -1:
                return new EmptyViewHolder(inflater.inflate(R.layout.item_empty,parent,false));
            case 1:
                return new NormalViewHolder(inflater.inflate(R.layout.item_warning,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case -1:
                ((EmptyViewHolder) holder).tv_empty.setText("暂无报障，下拉可刷新！");
                break;
            case 1:
                bindNormalHolder((NormalViewHolder) holder, position);
                break;
        }
    }

    public void bindNormalHolder(NormalViewHolder holder, final int position) {
        NormalViewHolder normalViewHolder = holder;
        final WarningItem warningItem = datas.get(position);
        if (warningItem!=null) {
            normalViewHolder.tv_house_name.setText(StringUtils.isEmpty(warningItem.house_name)?"未知房屋":warningItem.house_name);
            normalViewHolder.tv_c_timestamp.setText(
                    sdf.format(new Date(Long.parseLong(StringUtils.isEmpty(warningItem.c_timestamp)?"0":warningItem.c_timestamp))));
        }
        if (listenter!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listenter.onItemClick(warningItem,position,v);
                }
            });
        }
        String w_state = "未知";
        int stateColor= ContextCompat.getColor(context,R.color.gray_l);
        switch (warningItem.w_state) {
            case "1":
                w_state = "待派单";
                stateColor= ContextCompat.getColor(context,R.color.w_state_1);
                break;
            case "2":
                w_state = "待处理";
                stateColor= ContextCompat.getColor(context,R.color.w_state_2);
                break;
            case "3":
                w_state = "待确认";
                stateColor= ContextCompat.getColor(context,R.color.w_state_3);
                break;
            case "4":
                w_state = "已完成";
                stateColor= ContextCompat.getColor(context,R.color.w_state_4);
                break;
        }
        holder.tv_warning_state.setText(w_state);
        holder.tv_warning_state.setTextColor(stateColor);
    }

    @Override
    public int getItemViewType(int position) {
        return datas==null||datas.size()==0?-1:1;
    }

    @Override
    public int getItemCount() {
        return datas==null||datas.size()==0?1:datas.size();
    }
    private class NormalViewHolder extends RecyclerView.ViewHolder{

        private final TextView tv_house_name;
        private final TextView tv_c_timestamp;
        private final TextView tv_warning_state;

        public NormalViewHolder(View itemView) {
            super(itemView);
            tv_house_name = ((TextView) itemView.findViewById(R.id.tv_house_name));
            tv_c_timestamp = ((TextView) itemView.findViewById(R.id.tv_c_timestamp));
            tv_warning_state = ((TextView) itemView.findViewById(R.id.tv_warning_state));
        }
    }
    private class EmptyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_empty;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            tv_empty = ((TextView) itemView.findViewById(R.id.tv_empty));
        }

    }

    public OnRvItemClickListenter getListenter() {
        return listenter;
    }

    public void setListenter(OnRvItemClickListenter<WarningItem> listenter) {
        this.listenter = listenter;
    }
}
